package com.anycc.cloud.core.controller;

import com.anycc.cloud.common.dto.datatable.DataTableRequest;
import com.anycc.cloud.common.dto.datatable.DataTableResponse;
import com.anycc.cloud.common.dto.json.Response;
import com.anycc.cloud.common.dto.json.SuccessResponse;
import com.anycc.cloud.common.dto.query.OrderBy;
import com.anycc.cloud.core.entity.Role;
import com.anycc.cloud.core.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * role controller
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * create a new role
     *
     * @param role role
     * @return Response
     */
    @PostMapping(value = "create")
    public Response create(Role role) {
        roleService.create(role);
        return new SuccessResponse();
    }

    /**
     * datatable paging query
     *
     * @param role    role
     * @param request DataTableRequest
     * @return DataTableResponse
     */
    @GetMapping("findByPaging")
    public DataTableResponse<Role> findByPaging(Role role, DataTableRequest request) {
        List<OrderBy> orders = new ArrayList<>();
        orders.add(new OrderBy("desc", "createTime"));
        return new DataTableResponse<Role>(roleService.findByPaging(role, request.buildPageRequest(orders)), request);
    }

    /**
     * delete role by id
     *
     * @param id id
     * @return response
     */
    @PostMapping("delete")
    public Response delete(@RequestParam("id") String id) {
        this.roleService.delete(id);
        return new SuccessResponse();
    }

    /**
     * find role by id
     *
     * @param id id
     * @return Role
     */
    @GetMapping("findById")
    public Role findById(@RequestParam("id") String id) {
        return roleService.findById(id);
    }

    /**
     * update role
     *
     * @param role role
     * @return response
     */
    @PostMapping("update")
    public Response update(Role role) {
        roleService.update(role);
        return new SuccessResponse();
    }

    /**
     * find all role(asc)
     *
     * @return List
     */
    @GetMapping("findRoles")
    public List<Role> findAllByNameAsc() {
        return roleService.findAllByNameAsc();
    }
}